// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DBOrderServiceHelpers.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge.DataModel
{
    #region Usings

    using System;
    using System.Linq;

    #endregion

    /// <summary>
    /// The db order service helpers.
    /// </summary>
    public partial class DBBackOfficeHelpers
    {
        #region Public Methods

        /// <summary>
        /// The create order service.
        /// </summary>
        /// <param name="orderService">
        /// The orderService.
        /// </param>
        /// <returns>
        /// The create order service.
        /// </returns>
        public static bool CreateOrderService(Guid orderService)
        {
            var dbBBackOffice = DBBackOffice.CreateDBBackOffice(orderService);
            Singleton<DBForge>.Instance.Entities.AddToDBBackOffice(dbBBackOffice);

            return Singleton<DBForge>.Instance.Commit() > 0;
        }

        /// <summary>
        /// The exists order service.
        /// </summary>
        /// <param name="orderService">
        /// The orderService.
        /// </param>
        /// <returns>
        /// The exists order service.
        /// </returns>
        public static bool ExistsOrderService(Guid orderService)
        {
            var dbDictionaries = from d in Singleton<DBForge>.Instance.Entities.DBBackOffice where d.ID == orderService select d;

            return dbDictionaries.Count() == 1;
        }

        /// <summary>
        /// The get db order service.
        /// </summary>
        /// <param name="orderService">
        /// The orderService.
        /// </param>
        /// <returns>
        /// </returns>
        public static DBBackOffice GetDBBackOffice(Guid orderService)
        {
            var dbDictionaries = from d in Singleton<DBForge>.Instance.Entities.DBBackOffice where d.ID == orderService select d;

            if (dbDictionaries.Count() == 1)
            {
                return dbDictionaries.First();
            }

            return null;
        }

        #endregion
    }
}